package com.engine.cese.duss.singleLogin.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.singleLogin.util.LoginUtil;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaSingleLoginAction {

   /**
   *@Description: 单点登录接口
   *@Param: [request, response]
   *@return: java.lang.String
   *@Author: Dusongsong
   *@Date: 2021/3/10
   */
    @GET
    @Path("/singleLogin")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSingleLogin(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        BaseBean baseBean = new BaseBean();
        try {
            User user = HrmUserVarify.getUser( request, response );
            String loginid = user.getLoginid();
            JSONObject json = new JSONObject();
            json.put( "empCode", loginid);
            String param = json.toJSONString();
            JSONArray datasList = new JSONArray();
            String URL = baseBean.getPropValue( "ssoProp", "url" );
            JSONObject result = LoginUtil.sendPostHttpRequest( URL, param );
            JSONObject res = (JSONObject) result.get( "response" );
            JSONArray dataList = (JSONArray) res.get( "dataList" );
            String str ="http://sso.cese2.com:8035/sso-server/login?service=";
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject data = new JSONObject();
                String appName = (String) dataList.getJSONObject( i ).get( "appName" );
                String url = (String) dataList.getJSONObject( i ).get( "url" );
                data.put( "appName", appName );
                data.put( "key", i );
                data.put( "url", str+url );
                datasList.add( data );
            }
            apidatas.put( "datas", datasList );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "error_message", "请求异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
